/**
 * 
 */
package de.jare.photoswapper;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.ProgressDialog;
import android.graphics.BitmapFactory;
import android.os.Environment;

/**
 * @author jan
 * 
 */
public class HttpHelper2 {

	private void UploadFile(String FileName) {
		try {
			String lineEnd = "\r\n";
			String twoHyphens = "--";
			String boundary = "*****MyMultiPartBoundary";
			int len = 0;
			String appversion = "1.0";

			File sdcard = Environment.getExternalStorageDirectory();
			File mydir = new File(sdcard, "MyFileDir");
			File file = new File(mydir, FileName);

			FileInputStream fileInputStream = new FileInputStream(file);

			String UPLOADSCRIPT_URL = "http://www.example.com/upload.php?username=example&password=example";
			System.out.println("Opening: " + UPLOADSCRIPT_URL);
			URL url = new URL(UPLOADSCRIPT_URL);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();

			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setUseCaches(false);

			conn.setRequestMethod("POST");
			conn.setRequestProperty("User-Agent", "MyExampleApp/" + appversion);
			conn.setRequestProperty("Content-Type",
					"multipart/form-data;boundary=" + boundary);
			conn.connect();
			DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
			dos.writeBytes(twoHyphens + boundary + lineEnd);
			dos
					.writeBytes("Content-Disposition: form-data;	name=\"uploadedfile\";filename=\""
							+ FileName + "\"" + lineEnd);
			dos.writeBytes(lineEnd);

			int bytesAvailable;

			while ((bytesAvailable = fileInputStream.available()) > 0) {
				int bufferSize = Math.min(bytesAvailable, 4096);
				byte[] buffer = new byte[bufferSize];
				int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
				dos.write(buffer, 0, bytesRead);
			}

			dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
			fileInputStream.close();
			dos.flush();
			dos.close();
			DataInputStream dis = new DataInputStream(conn.getInputStream());
			byte[] data = new byte[1024];
			len = dis.read(data, 0, 1024);
			dis.close();
			int respcode = conn.getResponseCode();
			System.out.println("done uploading! resp code = " + respcode);
			String respstr = "FAILED";
			if (len > 0)
				respstr = new String(data, 0, len);

			if (respcode == 200 && respstr.equals("OK"))
				System.out.println("Uploaded " + FileName + " successfully!");
			else
				System.out.println("Upload Error: respcode=" + respcode
						+ ", respstr=" + respstr);
		} catch (Exception e) {
			System.out.println("upload error: " + e.toString());
		}
	}
	
	public void DownloadFile() {
    	
    	//pdlg = ProgressDialog.show(this, "Receiving picture", "Wait a second...", false, true);
        
    	new Thread() {
    		public void run () {
	    		URL myFileUrl = null;
	    		try {
					myFileUrl = new URL("http://swap.ganzbunt.de/get.php?AndroidId=" + HttpHandler.AndroidID + "&IMEI=" + HttpHandler.DeviceID + "&MobileNr=" + HttpHandler.MobileNr);
				} catch (MalformedURLException e1) {
					e1.printStackTrace();
				}
	    		try {
	                HttpURLConnection conn = (HttpURLConnection)myFileUrl.openConnection();
	                conn.setDoInput(true);
	                conn.connect();
	                InputStream is = conn.getInputStream();
	                //bmImg = BitmapFactory.decodeStream(is);
	                //handler.sendEmptyMessage(0);
	                
	    		} catch (IOException e) {
	                // TODO Auto-generated catch block
	                e.printStackTrace();
	           	}
    		}
    	}.start();
	}

}
